<template>
  <div style="width: 500px;">
    <!--文本搜索框-->
    <el-input
        prefix-icon="el-icon-search"
        placeholder="请输入部门名称进行搜索..."
        v-model="filterText">
    </el-input>

    <!--树形组件-->
    <el-tree
        class="filter-tree"
        :data="deps"
        :props="defaultProps"
        :expand-on-click-node="false"
        :filter-node-method="filterNode"
        ref="tree">

        <!--按钮-->
        <span style="display: flex; justify-content: space-between;width: 100%;" class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ data.name }}</span>
        <span>
          <el-button
              type="primary"
              class="depBtn"
              size="mini"
              @click="() => showAddDep(data)">
            添加部门
          </el-button>
          <el-button
              type="danger"
              class="depBtn"
              size="mini"
              @click="() => onDeleteDep(data)">
            删除部门
          </el-button>
        </span>
      </span>
    </el-tree>

    <!--对话框-->
    <el-dialog
        title="添加部门"
        :visible.sync="dialogVisible"
        width="30%">
      <div>
        <table>
          <tr>
            <td>
              <el-tag>上级部门</el-tag>
            </td>
            <td>{{ pname }}</td>
          </tr>
          <tr>
            <td>
              <el-tag>部门名称</el-tag>
            </td>
            <td>
              <el-input v-model="dep.name" placeholder="请输入部门名称..."></el-input>
            </td>
          </tr>
        </table>

      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onAddDep">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "DeptMana",
  data(){
    return {
      filterText: '',
      deps: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      dialogVisible: false,
      dep: {
        name: '',
        parentId: -1
      },
      pname: ''
    }
  },
  methods: {
    //初始化数据
    initDeps(){
      this.getRequest('/system/basic/department/').then(res => {
        if (res){
          this.deps = res
        }
      })
    },
    //显示添加部门对话框
    showAddDep(data){
      //为对话框添加上级部门的名称和父类的id
      this.dep.parentId = data.id
      this.pname = data.name
      this.dialogVisible = true
    },
    //手动删除节点
    removeDepFromDeps(p, deps, id){
      for (let i = 0; i < deps.length; i++) {
        let d = deps[i]
        if (d.id == id){
          deps.splice(i, 1)
          //没有子节点的时候其父部为false, 否则删除不了父节点，其数据没有更新，手动删除操作
          if (deps.length == 0){
            p.isParent = false
          }
          return
        }else {
          //递归遍历子节点
          this.removeDepFromDeps(d, d.children, id)
        }
      }
    },
    //删除部门
    onDeleteDep(data){
      //父部门不可以删除
      if (data.isParent){
        this.$message.error('父部门删除失败！')
      }else {
        this.$confirm('此操作将永久删除' + data.name +'部门, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.deleteRequest('/system/basic/department/' + data.id).then(res => {
            if (res){
              //手动删除节点
              this.removeDepFromDeps(null, this.deps, data.id)
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      }
    },
    initDep(){
      this.dep = {
        name: '',
        parentId: '-1'
      }
      this.pname = ''
    },
    //添加节点到其父节点下面，用于添加部门处理
    addDepToDeps(deps, dep){
      for (let i = 0; i <deps.length; i++) {
        let d = deps[i]
        if (d.id == dep.parentId){
          //添加数据到父节点下面
          d.children = d.children.concat(dep)
          //判断该节点下面是否还用子节点，有的话就将isParent置为true, 此处没有重新刷新数据库，是手动插入所以要手动修改
          if (d.children.length > 0){
            d.isParent = true
          }
          return
        }else {
         //否则就递归循环其子节点
         this.addDepToDeps(d.children, dep)
        }
      }
    },
    //添加部门
    onAddDep(){
      this.postRequest('/system/basic/department/', this.dep).then(res => {
        if (res){
          //使用初始化数据会折叠树形控件
          //手动插入刚才添加的数据，关闭对话框
          this.addDepToDeps(this.deps, res.obj)
          this.dialogVisible = false
          //清除之前的数据
          this.initDep()
        }
      })
    },
    filterNode(value, data) {
      //过滤节点，没有值的时候显示全部的数据
      if (!value) return true;
      //根据data的name来判断，显示对应的值
      return data.name.indexOf(value) !== -1;
    }
  },
  watch: {
    //文本框数据绑定事件
    filterText(val) {
      //调用树形控件的过滤方法
      this.$refs.tree.filter(val);
    }
  },
  mounted() {
    this.initDeps()
  }
}
</script>

<style scoped>
.depBtn {
  padding: 2px;
}

</style>
